package com.bytedance.improvingroad.widget

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bytedance.improvingroad.adapter.RVActivityListAdapter
import com.bytedance.improvingroad.utils.Utils
import kotlin.math.min

/**
 * Created by chenxiong
 * date 11/12/21
 */
class RVTestDecoration(): RecyclerView.ItemDecoration() {

    private val groupHeadHeight = Utils.dp2px(50)
    private val mHeadPaint: Paint by lazy {
        Paint().apply {
            color = Color.RED
            style = Paint.Style.FILL
        }
    }
    private val mTextPaint: Paint by lazy {
        Paint().apply {
            color = Color.WHITE
            style = Paint.Style.FILL
            textSize = 100f
        }
    }
    private val linePaint: Paint by lazy {
        Paint().apply {
            color = Color.WHITE
            style = Paint.Style.FILL
        }
    }
    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)

        if (parent.adapter is RVActivityListAdapter) {
            val rvAdapter = parent.adapter as RVActivityListAdapter
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            for (i in 0 until parent.childCount) {
                val view = parent.getChildAt(i)
                val pos = parent.getChildAdapterPosition(view)
                val isHeader = rvAdapter.isGroupHead(pos)


                if (isHeader && view.top - groupHeadHeight - parent.paddingTop >= 0) {
                    c.drawRect(
                        left.toFloat(),
                        (view.top - groupHeadHeight).toFloat(), right.toFloat(),
                        view.top.toFloat(),mHeadPaint)
//                    c.drawLine(left.toFloat(), (view.top-groupHeadHeight/2).toFloat(),right.toFloat(),(view.top-groupHeadHeight/2).toFloat(),linePaint)
                    val text = rvAdapter.dataList[pos].groupName
                    val metrics = mTextPaint.fontMetrics
                    val space = (metrics.descent - metrics.ascent) / 2 - metrics.descent
                    val baseLine = view.top- groupHeadHeight / 2 + space
                    c.drawText(text, (left+Utils.dp2px(20)).toFloat(),baseLine,mTextPaint)
                } else if (view.top - groupHeadHeight - parent.paddingTop >= 0) {
                    c.drawRect(left.toFloat(), view.top-1f,right.toFloat(),view.top.toFloat(),linePaint)
                }
            }
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)

        if (parent.adapter is RVActivityListAdapter) {
            val rvAdapter = parent.adapter as RVActivityListAdapter
            val left = (parent.paddingLeft).toFloat()
            val right = (parent.width - parent.paddingRight).toFloat()
            val top = parent.paddingTop.toFloat()
            (parent.layoutManager as? LinearLayoutManager)?.findFirstVisibleItemPosition()?.let { pos->
                parent.findViewHolderForAdapterPosition(pos)?.itemView?.let { view->
                    val isHeader = rvAdapter.isGroupHead(pos+1)
                    if (isHeader) {
                        val bottom = min(groupHeadHeight, (view.bottom - top).toInt())
                        c.drawRect(left, top, right, top+bottom,mHeadPaint)
                        val text = rvAdapter.dataList[pos].groupName
                        val metrics = mTextPaint.fontMetrics
                        val space = (metrics.descent - metrics.ascent) / 2 - metrics.descent
                        val baseLine = top + bottom - groupHeadHeight / 2 + space
                        c.clipRect(left,top,right,top+bottom)
                        c.drawText(text, left+Utils.dp2px(20),baseLine,mTextPaint)
                    } else {
                        c.drawRect(left, top, right, top+groupHeadHeight,mHeadPaint)
                        val text = rvAdapter.dataList[pos].groupName
                        val metrics = mTextPaint.fontMetrics
                        val space = (metrics.descent - metrics.ascent) / 2 - metrics.descent
                        val baseLine = top + groupHeadHeight / 2 + space
                        c.drawText(text, left+Utils.dp2px(20),baseLine,mTextPaint)
                    }
                }
            }

        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.adapter is RVActivityListAdapter) {
            val rvAdapter = parent.adapter as RVActivityListAdapter
            val pos = parent.getChildAdapterPosition(view)
            val isGroupHeader = rvAdapter.isGroupHead(pos)
            if (isGroupHeader) {
                outRect.set(0,groupHeadHeight,0,0)
            } else {
                outRect.set(0,1,0,0)
            }
        }

    }
}